import { useGlobalStore } from '@/store/modules/global'
import { DEFAULT_PRIMARY_COLOR } from '@/config'

export const useTheme = () => {
  const globalStore = useGlobalStore()
  const themeConfig = computed(() => globalStore.themeConfig)
  // 修改主题颜色
  const changePrimaryColor = (color) => {
    if (!color) {
      color = DEFAULT_PRIMARY_COLOR
    }
    globalStore.setThemeConfig({ ...themeConfig.value, primary: color })
    // 设置失效时间
    let curTime = new Date().getTime() + 3600 * 1000 * 24
    window.localStorage.setItem('THEME_COLOR', color, curTime)
    document.documentElement.style.setProperty('--primary-color', themeConfig.value.primary)
  }

  onBeforeMount(() => {
    changePrimaryColor(themeConfig.value.primary)
  })

  return {
    changePrimaryColor,
  }
}
